package ootexception;

import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;

public class ExceptionFrame extends Frame
                             implements ActionListener,
                                        WindowListener,
                                        ItemListener

{

MenuItem menu_item_exit;
MenuItem menu_item_raise;
MenuItem menu_item_runtime;
MenuItem menu_item_load;
MenuItem menu_item_save;
ExceptionDialog xcpt_dialog;

java.awt.List exceptionname;
java.awt.List keyname;

Label url_label;
Label msg_label;
Label xcpt_label;
Label key_label;

Button addException;
Button removeException;
Button addKeyname;
Button removeKeyname;
Button update;

TextArea message_area;
TextArea url_area;
TextField exception_field;
TextField keyname_field;
TextField current_field;

Hashtable ExceptionHashtable;
String key1;
String key2;
String message;
String url;
String current_key;

private void saveList(java.awt.List l,String filename)
{
try
{
PrintWriter pw=new PrintWriter(new FileWriter(filename));
int max=l.getItemCount();
int myloop=0;
for (myloop=0;myloop<max;myloop++)
  {
    String tally=l.getItem(myloop).toString();
    pw.println(tally);
  }
pw.close();
}
catch(Exception e){}
}

private void saveExceptions()
{
   saveList(exceptionname,"exceptions.dat");
}

private void saveKeynames()
{
   saveList(keyname,"keynames.dat");
}

private void saveHash()
{
try
{
PrintWriter pw=new PrintWriter(new FileWriter("hash.dat"));
PrintWriter pwk=new PrintWriter(new FileWriter("ExceptionArray.java"));

pwk.println("package ootexception;");
pwk.println("public class ExceptionArray");
pwk.println("{");
pwk.println();
pwk.println(" static String [][] hashdat=");
pwk.println("  {");


Enumeration elements=ExceptionHashtable.keys();

if (elements.hasMoreElements())
{
  String k=elements.nextElement().toString();
  ExceptionHashNode hn=(ExceptionHashNode) ExceptionHashtable.get(k);
  StringBuffer new_line=new StringBuffer();
  new_line.append(k);
  new_line.append("|");
  new_line.append(hn.msg);
  new_line.append("|");
  new_line.append(hn.url);
  new_line.append("&&");
  pw.println(new_line.toString());
  pwk.println("{");
  pwk.println("\""+k+"\",");
  pwk.println("\""+hn.msg+"\",");
  pwk.println("\""+hn.url+"\"");
  pwk.println("}");
}

while (elements.hasMoreElements())
{
  String k=elements.nextElement().toString();
  ExceptionHashNode hn=(ExceptionHashNode) ExceptionHashtable.get(k);
  StringBuffer new_line=new StringBuffer();
  new_line.append(k);
  new_line.append("|");
  new_line.append(hn.msg);
  new_line.append("|");
  new_line.append(hn.url);
  new_line.append("&&");
  pw.println(new_line.toString());
  pwk.println(",{");
  pwk.println("\""+k+"\",");
  pwk.println("\""+hn.msg+"\",");
  pwk.println("\""+hn.url+"\"");
  pwk.println("}");
}

pwk.println("};}");


pw.close();
pwk.close();
}
catch (Exception e){}


}

private java.awt.List loadList(java.awt.List l,String filename)
{
try
{
BufferedReader br=new BufferedReader(new FileReader(filename));
while (br.ready())
  {
    String tally=br.readLine();
    l.add(tally);
  }
br.close();
}
catch(Exception e){}
return l;
}

private void loadExceptions()
{
exceptionname=loadList(exceptionname,"exceptions.dat");
}


private void loadKeynames()
{
keyname=loadList(keyname,"keynames.dat");
}

private void loadHash()
{
new ExceptionHandler().loadFromFile("hash.dat");
ExceptionHashtable=ExceptionHandler.exception_hashtable;
}

private void doMenu()
{
MenuBar mb=new MenuBar();

Menu file_menu=new Menu("File");
Menu dialog_menu=new Menu("Dialog");

menu_item_exit=new MenuItem("Exit");
menu_item_load=new MenuItem("Load");
menu_item_save=new MenuItem("Save");
menu_item_raise=new MenuItem("Raise OOT_Exception");
menu_item_runtime=new MenuItem("Raise Java Exception");

file_menu.add(menu_item_load);
file_menu.add(menu_item_save);
file_menu.add(menu_item_exit);

dialog_menu.add(menu_item_raise);
dialog_menu.add(menu_item_runtime);

menu_item_exit.addActionListener(this);
menu_item_load.addActionListener(this);
menu_item_save.addActionListener(this);
menu_item_raise.addActionListener(this);
menu_item_runtime.addActionListener(this);

mb.add(file_menu);
mb.add(dialog_menu);

setMenuBar(mb);
}


private void arrangeLayout()
{


url_label=new Label("More Info Url");
msg_label=new Label("Dialog Message");
xcpt_label=new Label("Exception Class");
key_label=new Label("String Key");

addException=new Button("Add");
removeException=new Button("Remove");
addKeyname=new Button("Add");
removeKeyname=new Button("Remove");
update=new Button("Update");


addException.addActionListener(this);
removeException.addActionListener(this);
addKeyname.addActionListener(this);
removeKeyname.addActionListener(this);
update.addActionListener(this);



message_area=new TextArea("",0,0,TextArea.SCROLLBARS_NONE);
message_area.setEditable(true);
message_area.setBackground(Color.white);


url_area=new TextArea("",0,0,TextArea.SCROLLBARS_NONE);
url_area.setEditable(true);
url_area.setBackground(Color.white);

exception_field=new TextField();
exception_field.setEditable(true);
exception_field.setBackground(Color.white);

keyname_field=new TextField();
keyname_field.setEditable(true);
keyname_field.setBackground(Color.white);

current_field=new TextField();
current_field.setEditable(false);
current_field.setBackground(Color.white);

//----------------------------------------------------
setLayout(new BorderLayout());
Panel current_panel=new Panel();
Panel split_windows=new Panel();
add(current_panel,"North");
add(split_windows,"Center");
Panel top_two=new Panel();
Panel bottom_one=new Panel();
//----------------------------------------------------
split_windows.setLayout(new GridLayout(2,1));
split_windows.add(top_two);
split_windows.add(bottom_one);
//----------------------------------------------------
current_panel.setLayout(new GridLayout(2,1));
current_panel.add(new Label("Current table entry"));
Panel update_panel=new Panel();
current_panel.add(update_panel);
update_panel.setLayout(new BorderLayout());
update_panel.add(update,"West");
update_panel.add(current_field,"Center");
//----------------------------------------------------
top_two.setLayout(new GridLayout(1,2));
Panel exceptionPanel=new Panel();
Panel keynamePanel=new Panel();
top_two.add(exceptionPanel);
top_two.add(keynamePanel);


//----------------------------------------------------
exceptionPanel.setLayout(new BorderLayout());
exceptionPanel.add( exceptionname,"Center");
Panel ecp=new Panel();
exceptionPanel.add(ecp,"North");

//-------------------------------------
ecp.setLayout(new GridLayout(3,1));
ecp.add( xcpt_label);
ecp.add( exception_field);
Panel ecpb=new Panel();
ecp.add(ecpb);
//-------------------------------------
ecpb.setLayout(new FlowLayout());
ecpb.add( addException);
ecpb.add( removeException);

//QQZ
//-----------------------------------
keynamePanel.setLayout(new BorderLayout());
keynamePanel.add( keyname,"Center");
Panel kcp=new Panel();
keynamePanel.add(kcp,"North");

//-------------------------------------
kcp.setLayout(new GridLayout(3,1));
kcp.add( key_label);
kcp.add( keyname_field);
Panel kcpb=new Panel();
kcp.add(kcpb);
//-------------------------------------
kcpb.setLayout(new FlowLayout());
kcpb.add( addKeyname);
kcpb.add( removeKeyname);

//----------------------------------
bottom_one.setLayout(new GridLayout(2,1));
Panel msgpanel=new Panel();
Panel urlpanel=new Panel();
bottom_one.add(msgpanel);
bottom_one.add(urlpanel);

//----------------------------------
msgpanel.setLayout(new BorderLayout());
msgpanel.add(msg_label,"North");
msgpanel.add(message_area,"Center");
//----------------------------------
urlpanel.setLayout(new BorderLayout());
urlpanel.add(url_label,"North");
urlpanel.add(url_area,"Center");


//-----------------------------------


}


public ExceptionFrame()
{

new ExceptionHandler().loadFromFile("hash.dat");

setLocation(250,250);
setSize(500,500);

key1=null;
key2=null;
message=null;
url=null;
current_key=null;

ExceptionHashtable=new Hashtable();

exceptionname=new java.awt.List (0,false);
exceptionname.setBackground(Color.white);
keyname=new java.awt.List (0,false);
keyname.setBackground(Color.white);

keyname.addItemListener(this);
exceptionname.addItemListener(this);
keyname.addActionListener(this);
exceptionname.addActionListener(this);


message_area=new TextArea();
url_area=new TextArea();

doMenu();
arrangeLayout();

validate();

}

public void update(Graphics g)
{
}

public void paint(Graphics g)
{
}

public void actionPerformed(ActionEvent e)
{

if (e.getSource()==menu_item_exit)
    menu_item_exit_action();
else
if (e.getSource()==menu_item_raise)
    menu_item_raise_action();
else if (e.getSource()==menu_item_runtime)
    menu_item_runtime_action();
else if (e.getSource()==addException)
    button_addException_action();
else if (e.getSource()==removeException)
    button_removeException_action();
else if (e.getSource()==addKeyname)
    button_addKeyname_action();
else if (e.getSource()==removeKeyname)
    button_removeKeyname_action();
else if (e.getSource()==update)
    button_update_action();
else if (e.getSource()==menu_item_load)
    menu_item_load_action();
else if (e.getSource()==menu_item_save)
    menu_item_save_action();
repaint();
}

private void menu_item_load_action()
{
loadExceptions();
loadKeynames();
loadHash();
}

private void menu_item_save_action()
{
saveExceptions();
saveKeynames();
saveHash();
}

private void button_removeKeyname_action()
{
  String k=keyname_field.getText();
  int ref=getIndex(keyname,k);
  if (ref>=0)
    keyname.remove(ref);
  keyname.validate();
}

private void button_addKeyname_action()
{
  String k=keyname_field.getText();
  if (!inList(keyname,k))
    keyname.add(k);
}

private void button_removeException_action()
{
  String k=exception_field.getText();
  int ref=getIndex(exceptionname,k);
  if (ref>=0)
    exceptionname.remove(ref);
  exceptionname.repaint();
}




private void button_addException_action()
{
String k=exception_field.getText();
if (!inList(exceptionname,k))
  exceptionname.add(k);
  exceptionname.repaint();
}

public boolean inList( java.awt.List l,String s)
{
  int max=l.getItemCount();
  int myloop=0;
  boolean result=false;

  for (myloop=0;myloop<max;myloop++)
  {
     if (s.equals(
              l.getItem(myloop).toString()
        ))
      {
      result=true;
      break;
      }
  }
  return result;
}



public int getIndex( java.awt.List l,String s)
{
  int max=l.getItemCount();
  int myloop=0;
  int result=-1;

  for (myloop=0;myloop<max;myloop++)
  {
     if (s.equals(
              l.getItem(myloop).toString()
        ))
      {
      result=myloop;
      break;
      }
  }
  return result;
}



public void button_update_action()
{
StringBuffer sb=new StringBuffer();
message=message_area.getText();
url=url_area.getText();

if (key1==null) return;
if (key2==null) return;

sb.append(key1);
sb.append(key2);
sb.append("|");
sb.append(message);
sb.append("|");
sb.append(url);
sb.append("&&");

String display=sb.toString();

current_field.setText(display);
ExceptionHashNode new_node=new ExceptionHashNode(message,url);
String key=new String(key1+key2);
ExceptionHashtable.put(key,new_node);
ExceptionHandler.exception_hashtable.put(key,new_node);

}


public void menuitem_save_action()
{
}

public void menuitem_load_action()
{
}

public void menu_item_raise_action()
{
  ExceptionProcess process=new ExceptionProcess();

  try
  {
    process.go();
  }
  catch (Exception e)
  {
      ExceptionHandler.showException(this,e);
  }

}

public void menu_item_runtime_action()
{
  ExceptionRuntime process=new ExceptionRuntime();

  try
  {
    process.go();
  }
  catch (Exception e)
  {
      ExceptionHandler.showException(this,e);
  }

}



public void menu_item_exit_action()
{
  System.exit(1);
}
/**************************************************
**************************************************/

public void windowOpened(WindowEvent e)
{
}

public void windowClosing(WindowEvent e)
{
}

public void windowClosed(WindowEvent e)
{
}


public void windowActivated(WindowEvent e)
{
}

public void windowDeactivated(WindowEvent e)
{
}

public void windowIconified(WindowEvent e)
{
}

public void windowDeiconified(WindowEvent e)
{
}
/**************************************************
**************************************************/

public void itemStateChanged(ItemEvent e)
{
  Object comp_target=e.getItemSelectable();

  if (comp_target.equals(keyname))
  {
     java.awt.List temp_list=( java.awt.List )comp_target;
     Object target=temp_list.getItem(temp_list.getSelectedIndex());
     key2=target.toString();
     keyname_field.setText(key2);

  }
  if (comp_target.equals(exceptionname))
  {
     java.awt.List temp_list=( java.awt.List )comp_target;
     Object target=temp_list.getItem(temp_list.getSelectedIndex());
     key1=target.toString();
     exception_field.setText(key1);
  }
  hashCurrentKey();
  return;
}

public void hashCurrentKey()
{
  String keyid=new String(key1+key2);
  Object x=ExceptionHashtable.get(keyid);
  if (x!=null)
  {
  ExceptionHashNode node1=(ExceptionHashNode) x;
  String msgk=node1.msg;
  String urlk=node1.url;



  if (msgk!=null) message_area.setText(msgk); else
  message_area.setText("");
  if (urlk!=null) url_area.setText(urlk);
  else
  url_area.setText("");
  }
  else
  {
  message_area.setText("");
  url_area.setText("");
  }
  return;
}






}
